package senior.ioExer2;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

/**
 * 输入一个文件名，输出他的文本内容
 * @author shkstart
 * @creat 2022-04-14:33
 */
public class Exer2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入文件名：");
        String srcFile = sc.next();
        File file = new File(srcFile);
        if(!file.isFile()){
            System.out.println("不是文件");
            return;
        }
        String path = file.getAbsolutePath();

        FileReader fr = null;
        try {
            fr = new FileReader(path);
            char[] chars = new char[1024];
            int len;
            while ((len = fr.read(chars)) != -1){
                String str = new String(chars,0,len);
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fr != null){
                try {
                    fr.close();
                    System.out.println("操作成功");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
